<?php
include "../lib/DAL/MonCTDT.php";
class MonCTDTBLL{
    function __construct(){
        
    }
    
    function __destruct(){
        
    }
    public function getMonCTDT($mon,$nganh){
      $result = mysql_query("SELECT ma_mon_ctdt     FROM tbl_ctdt,tbl_mon_ctdt
                                                    WHERE tbl_ctdt.ma_ctdt=tbl_mon_ctdt.ma_ctdt
                                                    AND madm_nganh='$nganh'
                                                    AND ma_monhoc='$mon' ");
      $kq=mysql_fetch_array($result);
      return $kq['ma_mon_ctdt'];
      }
    public function setMonCTDTBLL($row){
        $MonCTDT = new MonCTDT();
        $MonCTDT->setMonCTDTDAL($row['donvitinh'],$row['khoiluong'],$row['ma_ctdt'],$row['ma_loaimonhoc'],$row['ma_monhoc']);
        return $MonCTDT;
    }
    public function setAllCTDT($row){
        $allCTDT=new MonCTDT();
        $allCTDT->setAllCTDT($row['ma_mon_ctdt'],$row['ten_lmh'],$row['ten_mh'],$row['donvitinh'],$row['khoiluong'],$row['ten_td'],$row['ten_hedt'],$row['ten_nganh']);
        return $allCTDT;
    }
    public function getAll(){
        $sql="SELECT MC.ma_mon_ctdt,L.ten_lmh, M.ten_mh, MC.donvitinh, MC.khoiluong,T.ten_td,H.ten_hedt,N.ten_nganh
              FROM tbl_mon_ctdt as MC 
              INNER JOIN dm_loaimonhoc as L ON MC.ma_loaimonhoc=L.madm_lmh
              INNER JOIN dm_monhoc AS M ON MC.ma_monhoc=M.madm_mh 
              INNER JOIN tbl_ctdt AS CT ON MC.ma_ctdt=CT.ma_ctdt
              INNER JOIN dm_trinhdo as T ON CT.madm_td=T.madm_td
              INNER JOIN dm_hedaotao as H ON CT.madm_hdt=H.madm_hdt
              INNER JOIN dm_nganh as N ON CT.madm_nganh=N.madm_nganh
              ORDER BY MC.ma_mon_ctdt DESC";
        $result = mysql_query($sql) or die(mysql_error());
        $arr_ctdt = array();
        while($row = mysql_fetch_array($result)){
            $arr_ctdt[] = $this->setAllCTDT($row);
        }
        return $arr_ctdt;
    }
    public function setlistmonCTDT($row){
        $MonCTDT = new MonCTDT();
        $MonCTDT->setlistmonCTDT($row['donvitinh'],$row['khoiluong'],$row['ma_loaimonhoc'],$row['ma_monhoc'],$row['ten_mh'], $row['ma_mon_ctdt']);
        return $MonCTDT;
    }
    public function getListMonCTDT($ma_ctdt){
        $sql =" SELECT MC.donvitinh, MC.khoiluong, MC.ma_loaimonhoc, MC.ma_monhoc, M.ten_mh, MC.ma_mon_ctdt
                FROM tbl_mon_ctdt AS MC 
                LEFT JOIN dm_monhoc AS M ON MC.ma_monhoc=M.madm_mh
                WHERE ma_ctdt LIKE '".$ma_ctdt."'
                ORDER BY M.ten_mh ASC";
        $rs = mysql_query($sql) or die(mysql_error());
        $arr_ctdt = array();
        while($row = mysql_fetch_array($rs)){
            $arr_ctdt[] = $this->setlistmonCTDT($row);
        }
        return $arr_ctdt;
    }
    public function insertMonCTDT($ctdt){
        $sql =" INSERT INTO tbl_mon_ctdt(donvitinh,khoiluong,ma_loaimonhoc,ma_monhoc,ma_ctdt) 
                VALUES ('".$ctdt->getdonvitinh()."',
                        '".$ctdt->getkhoiluong()."',
                        '".$ctdt->getma_loaimonhoc()."',
                        '".$ctdt->getma_monhoc()."',
                        '".$ctdt->getma_ctdt()."'
                        )";
        echo $sql;
        $result = mysql_query($sql) or die (mysql_error());
        return $result;
    }
    public function updateMonCTDT($ma_mon_ctdt,$ma_loaimonhoc,$khoiluong,$donvitinh){
        $sql = "UPDATE tbl_mon_ctdt
                SET ma_loaimonhoc='".$ma_loaimonhoc."',
                    khoiluong='".$khoiluong."',
                    donvitinh='".$donvitinh."'
                WHERE ma_mon_ctdt='".$ma_mon_ctdt."'";
        $result=mysql_query($sql) or die (mysql_error());
        return $result;
    }
    public function xoaMonCTDT($ma_monhoc,$ma_ctdt){
        $sql="DELETE FROM tbl_mon_ctdt WHERE ma_monhoc = '".$ma_monhoc."' AND ma_ctdt='".$ma_ctdt."'";
        $result = mysql_query($sql) or die (mysql_error());
        return $result;
    }
    public function xoaMaCTDT($ma_ctdt){
        $sql="DELETE FROM tbl_mon_ctdt WHERE ma_ctdt = '".$ma_ctdt."'";
        $result = mysql_query($sql) or die (mysql_error());
        return $result;
    }
}
?>